<template>
    <div class="sysUser">
        <div class="buttons">
            <el-button icon="el-icon-plus" @click="addUser">添加</el-button>
            <el-button icon="el-icon-delete" @click="deleteUser">删除</el-button>
            <el-input style="flex:1;margin-left:100px" placeholder="请输入用户名或者手机号搜索" prefix-icon="el-icon-search" v-model="searchKey">
            </el-input>
        </div>
        <div class="sysUserTable">
            <el-skeleton style="width:100%;margin-top:15px;padding: 0px 10px" v-if="!isShowTable" :rows="15" animated />
            <el-table v-if="isShowTable" :data="tableData" style="width: 100%" ref="userTable">
                <el-table-column type="selection" label="序号">
                </el-table-column>
                <el-table-column prop="username" min-width="200px" width="350px" label="用户名">
                </el-table-column>
                <el-table-column prop="phone" min-width="200px" width="350px" label="手机号码">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button icon="el-icon-edit" @click="updateUser(scope.$index, scope.row)">编辑</el-button>
                        <el-button icon="el-icon-edit" @click="initPassword(scope.row)">初始化密码</el-button>
                        <el-button icon="el-icon-plus" @click="grantRolePermission(scope.$index, scope.row)">用户角色授权</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10,50,100, 200, 300, 400]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <add-user-dialog v-if="isShowAddUser" @confirm="confirm" :params="params" @close="close" />
        <select-role-dialog v-if="isShowRoleDialog" @confirm="confirm" :params="params" @close="close" />
    </div>
</template>
<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator'
    import { Action, Getter } from 'vuex-class'
    import logo from '@/components/common/images/logo.png'
    import { namespace } from 'vuex-class';
    import * as _ from 'lodash';
    import AdminApiActions from "@/components/api/admin-api-actions";
    import AddUserDialog from "@/components/admin-manager/user-role-manager/sys-user/add-user-dialog.vue";
    import SelectRoleDialog from "@/components/admin-manager/user-role-manager/sys-user/select-role-dialog.vue";
    @Component({
        components: {
            AddUserDialog,
            SelectRoleDialog
        }
    })
    export default class SysUser extends Vue {
        public isShowAddUser: boolean = false;
        public isShowRoleDialog: boolean = false;
        public tableData: Array < any >= new Array < any > ();
        public params: any = {};
        public total: Number = 0;
        public isShowTable: boolean = false;
        public pageSize: number = 10;
        public curPage: Number = 1;
        public searchKey: any = "";
        public scope: any = "";
        async created() {
            await this.initTableData();
        }
        public async initTableData() {
            let result: any = await AdminApiActions.getSysUserPage({ name: "", pageInfo: { pageSize: this.pageSize, pageNum: this.curPage } });
            this.tableData = result.data.list;
            this.total = result.data.total;
            this.isShowTable = true;
        }
        //编辑用户
        public updateUser(index: any, item: any) {
            this.params.type = "update";
            this.params.form = {
                id: item.id,
                username: item.username,
                phone: item.phone
            }
            this.isShowAddUser = true;
        }
        //初始化用户密码
        public async initPassword(row: any) {
            await AdminApiActions.initPassword(row);
        }
        //用户角色授权
        public grantRolePermission(index: any, item: any) {
            this.params.userId = item.id;
            this.isShowRoleDialog = true;
        }
        //添加用户
        public addUser() {
            this.params.form = {
                username: "",
                phone: ""
            }
            this.params.type = "add";
            this.isShowAddUser = true;
        }
        //删除用户
        public async deleteUser() {
            let userTable: any = this.$refs.userTable;
            let selectData: Array < any >= userTable.selection;
            await AdminApiActions.deleteUser(selectData);
            await this.initTableData();
        }
        //关闭弹出框
        public close() {
            this.isShowAddUser = false;
            this.isShowRoleDialog = false;
        }
        public async confirm() {
            this.isShowTable = true;
            this.isShowAddUser = false;
            this.isShowRoleDialog = false;
            await this.initTableData();
        }
        //每页数量改变
        public async handleSizeChange(pageSize: any) {
            this.isShowTable = false;
            this.pageSize = pageSize;
            await this.initTableData();
        }
        //页数改变
        public async handleCurrentChange(curPage: any) {
            this.isShowTable = false;
            this.curPage = curPage;
            await this.initTableData();
        }
    }
</script>
<style lang="less">
    .sysUser {
        width: 100%;
        background-color: white;
        height: 100%;

        .el-table_1_column_4 {
            .cell {
                min-width: 450px;
            }
        }

        .el-main {
            padding: 0px 20px;
        }

        .buttons {
            display: flex;
            flex-direction: row;
        }

        .sysUserTable {
            .el-table__body-wrapper {
                overflow-y: auto;
                height: calc(100% - 70px);
            }

            height: calc(100% - 60px);

            .el-table {
                height: calc(100% - 40px);
            }

            .el-pagination {
                margin-top: 12px;
            }
        }

        .el-dialog__body {
            padding: 5px 20px;
        }

        .el-dialog__footer {
            padding: 0px 20px 20px;
        }

        .el-form-item {
            display: flex;

            .el-form-item__label {
                width: 100px;
            }

            .el-form-item__content {
                flex: 1;
                margin-right: 40px;
            }
        }
    }
</style>